home *** CD-ROM | disk | FTP | other *** search
/ Shareware Super Platinum 8 / Shareware Super Platinum 8.iso / mac / PROGTOOL / FLI106C.ZIP;1 / FLIEVENT.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-12  |  10.5 KB  |  450 lines

  1. //
  2. // The Fusion Library Interface for DOS
  3. // Version 1.06c
  4. // Copyright (C) 1990, 1991, 1992
  5. // Software Dimensions
  6. //
  7. // EventClass
  8. //
  9.  
  10. #include "fli.h"
  11. #include "colors.h"
  12.  
  13. #ifdef __BCPLUSPLUS__
  14. #pragma hdrstop
  15. #endif
  16.  
  17. #include <alloc.h>
  18. #include <string.h>
  19. #include <bios.h>
  20.  
  21. int Event::EventTimer=16;
  22.  
  23. //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  24. //
  25. // Event()
  26. //
  27. // Constructor for Event class
  28. //
  29. //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  30.  
  31. static void (**GlobalActionQueue)();
  32. static int GlobalActionCount;
  33.  
  34. Event::Event()
  35. {
  36.   ActionQueue=NULL;
  37.   ActionCount=0;
  38.   X=0;
  39.   Y=0;
  40.   Width=0;
  41.   Height=0;
  42.   CloseIcon=1;
  43.   ActionTimer=0;
  44.   ScreenSave=NULL;
  45.   FlushMouseQueue();
  46.   XShadow=0;
  47.   YShadow=0;
  48.   Shadowing=0;
  49. }
  50.  
  51. //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  52. //
  53. // ~Event()
  54. //
  55. // Destructor for Event class
  56. //
  57. //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  58.  
  59. Event::~Event()
  60. {
  61.   RestoreWindow();
  62.  
  63.   if (ActionCount)
  64.     free(ActionQueue);
  65.  
  66.   FlushMouseQueue();
  67. }
  68.  
  69. //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  70. //
  71. // SpecifyWindow() / RestoreWindow()
  72. //
  73. // These functions specify a window region that can be moved or altered,
  74. // anything below these windows is saved upon a call to SpecifyWindow and
  75. // restored upon a call to RestoreWindow.
  76. //
  77. //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  78.  
  79. void Event::SpecifyWindow(int X,int Y,int Width,int Height)
  80. {
  81.   if (ScreenSave)
  82.     delete ScreenSave;
  83.  
  84.   ScreenSave=NULL;
  85.  
  86.   if (FusionShadowing)
  87.   {
  88.     Shadowing=1;
  89.     XShadow=0;
  90.     YShadow=0;
  91.  
  92.     if (X+Width<Blaze.WhatWidth())
  93.       XShadow++;
  94.  
  95.     if (Y+Height<Blaze.WhatHeight()-1)
  96.       YShadow++;
  97.  
  98.     if (Width==Blaze.WhatWidth() || Height==Blaze.WhatHeight()-2
  99.       || !XShadow || !YShadow)
  100.     {
  101.       XShadow=0;
  102.       YShadow=0;
  103.       Shadowing=0;
  104.     }
  105.  
  106.     ScreenSave=new char[Blaze.ComputeNeededBytes(Width+XShadow,Height+YShadow)];
  107.  
  108.     MouseHide();
  109.     Blaze.GetArea(X,Y,Width+XShadow,Height+YShadow,ScreenSave);
  110.     if (Shadowing)
  111.       Blaze.Shadow(X+XShadow,Y+YShadow,Width,Height);
  112.     MouseShow();
  113.   }
  114.   else
  115.   {
  116.     Shadowing=0;
  117.     ScreenSave=new char[Blaze.ComputeNeededBytes(Width,Height)];
  118.     MouseHide();
  119.     Blaze.GetArea(X,Y,Width,Height,ScreenSave);
  120.     MouseShow();
  121.   }
  122.  
  123.   Event::X=X;
  124.   Event::Y=Y;
  125.   Event::Width=Width;
  126.   Event::Height=Height;
  127. }
  128.  
  129. void Event::RestoreWindow()
  130. {
  131.   if (!ScreenSave)
  132.     return;
  133.  
  134.   MouseHide();
  135.   Blaze.PutArea(X,Y,ScreenSave);
  136.   MouseShow();
  137.   delete ScreenSave;
  138.   ScreenSave=NULL;
  139. }
  140.  
  141. //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  142. //
  143. // LocalLive()
  144. //
  145. // Adds a "LIVE" function onto the local "LIVE" function handler.
  146. // "LIVE" functions are automatically executed once every second.
  147. //
  148. //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  149.  
  150. void Event::LocalLive(void (*Action)())
  151. {
  152.   ActionCount++;
  153.   ActionQueue=(void(**)())realloc(ActionQueue,ActionCount*sizeof(void (*)()));
  154.   ActionQueue[ActionCount-1]=Action;
  155. }
  156.  
  157. //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  158. //
  159. // GlobalLive()
  160. //
  161. // Adds a "LIVE" function onto the global "LIVE" function handler.
  162. // Global "LIVE" functions are alive for all occurances of the event class.
  163. // "LIVE" functions are automatically executed once every second.
  164. //
  165. //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  166.  
  167. void Event::GlobalLive(void (*GlobalAction)())
  168. {
  169.   GlobalActionCount++;
  170.   GlobalActionQueue=
  171.     (void(**)())realloc(GlobalActionQueue,GlobalActionCount*sizeof(void (*)()));
  172.   GlobalActionQueue[GlobalActionCount-1]=GlobalAction;
  173. }
  174.  
  175. //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  176. //
  177. // NotClosable()
  178. //
  179. // Signals that there isn't a close button on this window
  180. //
  181. //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  182.  
  183. void Event::NotClosable()
  184. {
  185.   CloseIcon=0;
  186. }
  187.  
  188. //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  189. //
  190. // GetEvent()
  191. //
  192. // Checks for an event and returns one, if found.  All "LIVE" or action
  193. // events are executed when this function is idle (no keyboard or mouse
  194. // movements).  If the right position of a particular item (window) is
  195. // grabbed, then the window is moved, or closed.
  196. //
  197. //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  198.  
  199. extern int HelpSystemAvailable; // Is help available (i.e. the help file)
  200. extern int DisabledHelp; // Was the help system shut off by the programmer?
  201.  
  202. int Event::GetEvent(int SeeEvent)
  203. {
  204.   static int RememberButtonDown=0;
  205.   static long RememberDownDelay=0;
  206.  
  207. Top:
  208.  
  209.   if (!MouseButtons()&LeftButton)
  210.     RememberButtonDown=0;
  211.  
  212.   int LastShift=bioskey(2);
  213.   GetMouseQueue();
  214.  
  215.   while (!Blaze.SeeKey() && !MouseEvent && bioskey(2)==LastShift)
  216.   {
  217.     GetMouseQueue();
  218.  
  219.     if (MouseButtons()&LeftButton &&
  220.       RememberButtonDown &&
  221.       RememberDownDelay<biostime(0,0))
  222.     {
  223.       MouseEvent|=MouseHeldDown;
  224.       MouseLocate();
  225.       RememberDownDelay=biostime(0,0)+MouseRepeatSpeed;
  226.       return MousedEvent;
  227.     }
  228.  
  229.     if ((ActionCount || GlobalActionCount) && biostime(0,0)>(ActionTimer+EventTimer))
  230.     {
  231.       MouseHide();
  232.       if (GlobalActionCount)
  233.       {
  234.         for (int i=0;i<GlobalActionCount;i++)
  235.           (GlobalActionQueue[i])();
  236.       }
  237.       if (ActionCount)
  238.       {
  239.         for (int i=0;i<ActionCount;i++)
  240.           (ActionQueue[i])();
  241.       }
  242.       MouseShow();
  243.       ActionTimer=biostime(0,0);
  244.     }
  245.  
  246.     if (SeeEvent)
  247.     {
  248.       if (MouseEvent)
  249.         return MousedEvent;
  250.       return 0;
  251.     }
  252.   };
  253.  
  254.   if (Blaze.SeeKey()==kbF1 || MouseEvent&MouseRightButtonRelease)
  255.   {
  256.     if (DisabledHelp || !HelpSystemAvailable)
  257.       return (Blaze.SeeKey()==kbF1)?Blaze.GetKey():MousedEvent;
  258.     if (Blaze.SeeKey()==kbF1)
  259.       Blaze.GetKey();
  260.     EngageHelp();
  261.     return HelpEvent;
  262.   }
  263.  
  264.   if (Blaze.SeeKey())
  265.     return Blaze.GetKey();
  266.  
  267.   if (bioskey(2)!=LastShift)
  268.     return ShiftEvent;
  269.  
  270.   if (MouseEvent)
  271.   {
  272.     if (Width)
  273.     {
  274.       if (MouseEvent&MouseLeftButtonPress && MouseVertical==Y &&
  275.         ((CloseIcon && MouseHorizontal>=X+4 && MouseHorizontal<X+Width-1) ||
  276.          (!CloseIcon && MouseHorizontal>X && MouseHorizontal<X+Width-1)))
  277.       {
  278.         if (!Y) // protects against attempted moves top line
  279.           return MousedEvent;
  280.         MoveWindow();
  281.         return MoveEvent;
  282.       }
  283.  
  284.       if (MouseEvent&MouseLeftButtonRelease && CloseIcon &&
  285.         (MouseHorizontal==X+2 && MouseVertical==Y))
  286.         return CloseEvent;
  287.  
  288.       if (MouseEvent&MouseLeftButtonRelease &&
  289.         (MouseHorizontal<X || MouseHorizontal>=(X+Width) ||
  290.          MouseVertical<Y || MouseVertical>=(Y+Height)))
  291.         return OutsideEvent;
  292.     }
  293.  
  294.     if (MouseEvent&MouseLeftButtonPress)
  295.     {
  296.       RememberButtonDown=1;
  297.       RememberDownDelay=biostime(0,0)+MouseRepeatDelay;
  298.     }
  299.     else
  300.     {
  301.       if (!MouseButtonStatus&LeftButton)
  302.         RememberButtonDown=0;
  303.       else if (RememberButtonDown)
  304.         MouseEvent|=MouseHeldDown;
  305.     }
  306.  
  307.     return MousedEvent;
  308.   }
  309.  
  310.   return 0;
  311. }
  312.  
  313. //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  314. //
  315. // JustEvent()
  316. //
  317. // Just grab an event.  This function is used locally and is a private
  318. // member function.  It does not perform some of the extended mouse features
  319. // that are found in the GetEvent() member function.
  320. //
  321. //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  322.  
  323. int Event::JustEvent()
  324. {
  325.   GetMouseQueue();
  326.  
  327.   while (!Blaze.SeeKey() && !MouseEvent)
  328.   {
  329.     GetMouseQueue();
  330.     if ((ActionCount || GlobalActionCount) && biostime(0,0)>(ActionTimer+EventTimer))
  331.     {
  332.       if (ActionCount)
  333.       {
  334.         for (int i=0;i<ActionCount;i++)
  335.           (ActionQueue[i])();
  336.       }
  337.       if (GlobalActionCount)
  338.       {
  339.         for (int i=0;i<GlobalActionCount;i++)
  340.           (GlobalActionQueue[i])();
  341.       }
  342.       ActionTimer=biostime(0,0);
  343.     }
  344.   };
  345.  
  346.   if (Blaze.SeeKey())
  347.     return Blaze.GetKey();
  348.   else
  349.     return MousedEvent;
  350. }
  351.  
  352. //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  353. //
  354. // MoveWindow()
  355. //
  356. // Move the window.  This function is used locally and is a private member
  357. // function that is called from GetEvent() when a mouse grabs the
  358. // top of any window object.
  359. //
  360. //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  361.  
  362. void Event::MoveWindow()
  363. {
  364.   MouseHide();
  365.  
  366.   int ActionX=MouseHorizontal;
  367.   int Locate=ActionX-X;
  368.   int ActionY=MouseVertical;
  369.  
  370.   Blaze.Window(0,0,Blaze.WhatWidth(),Blaze.WhatHeight());
  371.   Blaze.InvisibleCursor();
  372.  
  373.   Blaze.HelpLine(0,"Use the mouse to move the window");
  374.  
  375.   char *EntireSave=
  376.     new char[Blaze.ComputeNeededBytes(Blaze.WhatWidth(),Blaze.WhatHeight())];
  377.   char *MenuSave=new char[Blaze.ComputeNeededBytes(Width+XShadow,Height+YShadow)];
  378.  
  379.   Blaze.CopyVisualToVirtual(EntireSave);
  380.  
  381.   Blaze.UseMemory(EntireSave);
  382.  
  383.   Blaze.GetArea(X,Y,Width,Height,MenuSave);
  384.  
  385.   MouseShow();
  386.  
  387.   int ExactX=MouseHorizontal-X;
  388.   int ExactY=MouseVertical-Y;
  389.  
  390.   for (;;)
  391.   {
  392.     JustEvent();
  393.  
  394.     if (MouseEvent&4)
  395.     {
  396.       delete EntireSave;
  397.       delete MenuSave;
  398.       Blaze.UseVideo();
  399.       return;
  400.     }
  401.  
  402.     if (MouseEvent&1)
  403.     {
  404.       MouseHide();
  405.  
  406.       Blaze.PutArea(X,Y,ScreenSave);
  407.  
  408.       if (ActionX>=(X+Locate))
  409.         X+=(MouseHorizontal-ActionX);
  410.       if (ActionY>=Y)
  411.         Y+=(MouseVertical-ActionY);
  412.  
  413.       if (X<0)
  414.         X=0;
  415.       if ((X+Width+XShadow)>Blaze.WhatWidth())
  416.         X=Blaze.WhatWidth()-Width-XShadow;
  417.  
  418.       if (Y<1)
  419.         Y=1;
  420.       if ((Y+Height+YShadow)>(Blaze.WhatHeight()-1))
  421.         Y=Blaze.WhatHeight()-1-Height-YShadow;
  422.  
  423.       ActionX=MouseHorizontal;
  424.       ActionY=MouseVertical;
  425.  
  426.       Blaze.GetArea(X,Y,Width+XShadow,Height+YShadow,ScreenSave);
  427.  
  428.       if (Shadowing)
  429.         Blaze.Shadow(X+XShadow,Y+YShadow,Width,Height);
  430.  
  431.       Blaze.PutArea(X,Y,MenuSave);
  432.  
  433.       Blaze.BlockCopyVirtualToVisual(0,1,Blaze.WhatWidth(),Blaze.WhatHeight()-2,
  434.         EntireSave);
  435.  
  436.       MouseShow();
  437.  
  438.      if (MouseVertical!=Y+ExactY ||
  439.        MouseHorizontal!=X+ExactX)
  440.      {
  441.        MousePosition(X+ExactX,Y+ExactY);
  442.        MouseLocate();
  443.        ActionX=MouseHorizontal;
  444.        ActionY=MouseVertical;
  445.      }
  446.     }
  447.   }
  448. }
  449.  
  450.